<?php
// contact to database
$connect = mysql_connect("localhost", "root", "") or die ("Error , check your server connection.");
mysql_select_db("stms");
 
//Get data in local variable
$Regno=$_POST['Regno'];
$Title=$_POST['Title'];
$Firstname=$_POST['Firstname'];
$Lastname=$_POST['Lastname'];
$NICno=$_POST['NICno'];
$radio=$_POST['radio'];
$Civilstatus=$_POST['Civilstatus'];
$DOB=$_POST['DOB'];
$Nationality=$_POST['Nationality'];
$Religion=$_POST['Religion'];
$PermanentAddress=$_POST['Permanent Address'];
$TemporaryAddress=$_POST['Temporary Address'];
$Faculty=$_POST['Faculty'];
$CourseOfStudy=$_POST['CourseOfStudy'];
$Academicyear=$_POST['Academic year'];
$District=$_POST['District'];


$result=mysql_query("SELECT * FROM basicdetails WHERE Regno='$Regno'");
 $insert="ADD";
 $update="UPDATE";
 $delete="DELETE";
 
 if($_POST['ADD']==$insert)
 {
 
 if (mysql_num_rows($result) == 1){
 echo "This Regno is already exist";
 }
 else
 {
// check for null values
	if ($Regno==""  or $Title=="")
	echo "All fields must be entered, hit back button and re-enter information";
	else{
	$query="insert into basicdetails(Regno,Title,Firstname,Lastname,NICno,Gender,Civilstatus,DOB,Nationality,Religion,PermanentAddress,TemporaryAddress,Faculty,CourseOfStudy,Academicyear,District) values('$Regno','$Title','$Firstname','$Lastname','$NICno','$radio','$Civilstatus','$DOB','$Nationality','$Religion','$PermanentAddress','$TemporaryAddress','$Faculty','$CourseOfStudy','$Academicyear','$District')";
	mysql_query($query)  or die(mysql_error());
	echo "Your message has been received";
	}
	}
}
else if($_POST['UPDATE']==$update)
{
if (mysql_num_rows($result) == 1)
{
	$upd="UPDATE basicdetails SET Title='$Title',Firstname='$Firstname',Lastname='$Lastname',NICno='$NICno',Gender='$radio',Civilstatus='$Civilstatus',DOB='$DOB',Nationality='Nationality',Religion='$Religion',PermanentAddress='$PermanentAddress',TemporaryAddress='$TemporaryAddress',Faculty='$Faculty',CourseOfStudy='$CourseOfStudy',Academicyear='$Academicyear',District='$District' WHERE Regno='$Regno'";

	mysql_query($upd)  or die(mysql_error());
	echo "Updated successfully";
 }
 
 else echo "There is no Regno as you given";
 }
 else if($_POST['DELETE']==$delete)
 {
 
if (mysql_num_rows($result) == 1)
 {
 $del="DELETE FROM basicdetails WHERE Regno = '$Regno'";
 mysql_query($del)  or die(mysql_error());
	echo "Deleted successfully";
	}
	else
	echo "Regno is not there";
	
 
 }
 
?>